<?php
/**
 * @title 676. 实现一个魔法字典 - 哈希
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class MagicDictionary {
    /**
     */
    function __construct() {
        $this->wordArray = $this->matchArray = [];
    }

    /**
     * @param String[] $dictionary
     * @return NULL
     */
    function buildDict($dictionary) {
        /**
         * @since 2021-12-02 迭代字典
         */
        foreach ($dictionary as $d){
            $this->wordArray[$d] = 1;

            /**
             * @since 2021-12-02 处理匹配字符串
             */
            for ($i=0; $i<strlen($d); $i++){
                $char = $d[$i];
                $d[$i] = "*";

                /**
                 * @since 2021-12-02 存储
                 */
                if(isset($this->matchArray[$d])){
                    $this->matchArray[$d] += 1;
                } else {
                    $this->matchArray[$d] = 1;
                }

                /**
                 * @since 2021-12-02 还原
                 */
                $d[$i] = $char;
            }
        }
    }

    /**
     * @param String $searchWord
     * @return Boolean
     */
    function search($searchWord) {
        /**
         * @since 2021-12-02 查询字符串
         */
        $sWord = $searchWord;

        /**
         * @since 2021-12-02 依次替换
         */
        for($i=0; $i<strlen($searchWord); $i++){
            /**
             * @since 2021-12-02 替换
             */
            $char = $searchWord[$i];
            $searchWord[$i] = "*";

            /**
             * @since 2021-12-02 是否匹配
             */
            if(isset($this->matchArray[$searchWord])){
                /**
                 * @since 2021-12-02 存在2个和以上
                 */
                if($this->matchArray[$searchWord] >= 2 OR !isset($this->wordArray[$sWord])){
                    return true;
                } else {}
            } else {}

            /**
             * @since 2021-12-02 还原
             */
            $searchWord[$i] = $char;
        }

        /**
         * @return
         */
        return false;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202112/2021.12.02_3.php
 */
$datas = [
    [
        ["MagicDictionary", "buildDict", "search", "search", "search", "search"],
        [[], [["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]],
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
// foreach ($datas as $data){
//     if(count($data) <= 20){
//         var_dump($data);
//     } else {
//         var_dump(count($data));
//     }
//
//     $obj = new Solution();
//     $result = $obj->(...$data);
//     // $result = $obj->($xhprof->tree($data));
//     // $result = $obj->($xhprof->listNode($data));
//     // $result = $obj->find(...$data);
//     var_dump($result);
//     // if(count($result)<=20){
//     //     var_dump($result);
//     // } else {
//     //     var_dump(count($result));
//     // }
//     echo str_repeat(PHP_EOL, 3);
//     echo str_repeat("<br>", 3);
// }

foreach ($datas as $data){
    $obj = new $data[0][0](...$data[1][0]);

    for ($i=1; $i<count($data[0]); $i++){
        $func = $data[0][$i];
        $param = $data[1][$i];
        $result = $obj->$func(...$param);

        echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
        if($result === true){
            echo "True";
        } elseif($result === false) {
            echo "False";
        } elseif($result === null) {
            echo "Null";
        } elseif(is_array($result)) {
            var_dump($result);
        } else {
            echo $result;
        }
        echo PHP_EOL;
    }

    echo str_repeat(PHP_EOL, 3);
}
$xhprof->end();
